package cn.jiedanba.cacert.common.ca.algorithm;

import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;

public class AlgorithmOID {
	/**
	 * Maps the digest IDs with the human-readable name of the digest algorithm.
	 */
	private static final HashMap<String, String> hashAlg = new HashMap<String, String>();

	static {
		hashAlg.put("1.2.840.113549.2.5", "MD5");
		hashAlg.put("1.3.14.3.2.26", "SHA1");
		hashAlg.put("2.16.840.1.101.3.4.2.1", "SHA256");
		hashAlg.put("2.16.840.1.101.3.4.2.2", "SHA384");
		hashAlg.put("2.16.840.1.101.3.4.2.3", "SHA512");
		hashAlg.put("1.2.840.113549.1.1.4", "MD5");
		hashAlg.put("1.2.840.113549.1.1.11", "SHA256");
		hashAlg.put("1.2.840.113549.1.1.12", "SHA384");
		hashAlg.put("1.2.840.113549.1.1.13", "SHA512");
		hashAlg.put("1.2.840.113549.2.5", "MD5");
		hashAlg.put("1.2.840.10040.4.3", "SHA1");
		hashAlg.put("2.16.840.1.101.3.4.3.2", "SHA256");
		hashAlg.put("2.16.840.1.101.3.4.3.3", "SHA384");
		hashAlg.put("2.16.840.1.101.3.4.3.4", "SHA512");
		hashAlg.put("1.2.156.10197.1.401", "SM3");

		hashAlg.put("MD5", "1.2.840.113549.2.5");
		hashAlg.put("MD-5", "1.2.840.113549.2.5");
		hashAlg.put("SHA1", "1.3.14.3.2.26");
		hashAlg.put("SHA-1", "1.3.14.3.2.26");
		hashAlg.put("SHA256", "2.16.840.1.101.3.4.2.1");
		hashAlg.put("SHA-256", "2.16.840.1.101.3.4.2.1");
		hashAlg.put("SHA384", "2.16.840.1.101.3.4.2.2");
		hashAlg.put("SHA-384", "2.16.840.1.101.3.4.2.2");
		hashAlg.put("SHA512", "2.16.840.1.101.3.4.2.3");
		hashAlg.put("SHA-512", "2.16.840.1.101.3.4.2.3");
		hashAlg.put("SM3", "1.2.156.10197.1.401");

	}

	/**
	 * 获取 hash算法的oid或者获取oid的算法名称
	 * 
	 * @param var
	 * @return oid or AlgName
	 */
	public static String getHashAlgOidOrHashName(String val) {
		if (StringUtils.isBlank(val)) {
			return null;
		}
		return hashAlg.get(val.toUpperCase());
	}

	public static ASN1ObjectIdentifier getOidASN1ObjectIdentifier(String hashName) {
		return new ASN1ObjectIdentifier(getHashAlgOidOrHashName(hashName));
	}

}
